﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Principal;
using System.Web;

using System.Text;

using System.Configuration;
using DataMongo;
using DataMongo.Entities;
using Quartz;


namespace BreakingHubAdmin.Services
{
    public class GetFeedsService : IJob
    {

        //Collects individual message and prepares a single message for email sending

        public void Execute(JobExecutionContext context)
        {
            GetRSSFeeds();

        }


        public static void GetRSSFeeds()
        {
            var sourcedetailconnection = new RepositoryBaseMongo("SourceDetail");
            var list = sourcedetailconnection.GetAll<SourceDetail>();
            var feedConnection = new RepositoryBaseMongo("FeedPost");

            foreach (var item in list)
            {
                var data = feedConnection.GetAll<FeedPost>().Select(x => x.SourceLink).ToList();
                var title = feedConnection.GetAll<FeedPost>().Select(x => x.Title).ToList();
                var rssFeeds = RssReader.GetRssFeed(item.FeedURL);

                if (rssFeeds != null)
                {
                    foreach (var rssFeed in rssFeeds.Where(x => !data.Contains(x.Link) && !title.Contains(x.Title)))
                    {
                        var connection = new RepositoryBaseMongo("FeedPost");
                        var feed = new FeedPost()
                        {
                            Title = rssFeed.Title,
                            ShortDescription = rssFeed.Description,
                            FullDescription = rssFeed.Description,
                            SourceId = item.SourceId,
                            CategoryId = item.CategoryId,
                            PublishDate = rssFeed.PublishDate,
                            SourceLink = rssFeed.Link,
                            ImageURL1 = rssFeed.ImageUrl,
                            IsActive = false
                        };
                        var imageUrl = UtilityService.GetImageUrlFromSite(feed.SourceLink);
                        if (imageUrl != null)
                        {
                            var path = AppDomain.CurrentDomain.BaseDirectory + "Content\\FeedImages\\";

                            if (!Directory.Exists(path))
                                Directory.CreateDirectory(path);
                            var imagepath = AppDomain.CurrentDomain.BaseDirectory + "Content\\FeedImages\\" +
                                            UtilityService.RemoveSpecialCharacters(feed.Title) + ".jpg";
                            bool result = UtilityService.DownloadImageFromUrl(imageUrl, imagepath);
                            if (result == true)
                            {

                                feed.ImageURL1 = UtilityService.RemoveSpecialCharacters(feed.Title) + ".jpg";
                            }
                            else
                            {
                                feed.ImageURL1 = null;
                            }
                        }
                        else
                        {
                            feed.ImageURL1 = null;
                        }
                        int Id = 0;
                        var feedlist = connection.GetAll<FeedPost>().ToList();
                        if (feedlist.Any())
                            Id = feedlist.Max(x => x.FeedPostId);
                        Id += 1;
                        feed.FeedPostId = Id;
                        connection.InsertOrUpdate(feed);
                    }
                }

            }
        }


        public static void GetRssFeedBySourceUrl(int id)
        {
            var sourceConnection = new RepositoryBaseMongo("SourceDetail");
            var feedConnection = new RepositoryBaseMongo("FeedPost");


            var sourceUrl = (from f in sourceConnection.GetAll<SourceDetail>()
                where f.SourceDetailId == id
                select f).SingleOrDefault();
            if (sourceUrl != null)
            {
                var data = feedConnection.GetAll<FeedPost>().Select(x => x.SourceLink).ToList();
                var title = feedConnection.GetAll<FeedPost>().Select(x => x.Title).ToList();

                var rssFeeds = RssReader.GetRssFeed(sourceUrl.FeedURL);
                if (rssFeeds != null)
                {
                    foreach (var rssFeed in rssFeeds.Where(x => !data.Contains(x.Link) && !title.Contains(x.Title)))
                    {
                        var connection = new RepositoryBaseMongo("FeedPost");
                        var feed = new FeedPost()
                        {
                            Title = rssFeed.Title,
                            ShortDescription = rssFeed.Description,
                            FullDescription = rssFeed.Description,
                            SourceId = sourceUrl.SourceId,
                            CategoryId = sourceUrl.CategoryId,
                            PublishDate = rssFeed.PublishDate,
                            SourceLink = rssFeed.Link,
                            ImageURL1 = rssFeed.ImageUrl,
                            IsActive = false
                        };
                        var imageUrl = UtilityService.GetImageUrlFromSite(feed.SourceLink);
                        if (imageUrl != null)
                        {
                            var path = AppDomain.CurrentDomain.BaseDirectory + "Content\\FeedImages\\";

                            if (!Directory.Exists(path))
                                Directory.CreateDirectory(path);
                            var imagepath = AppDomain.CurrentDomain.BaseDirectory + "Content\\FeedImages\\" +
                                            UtilityService.RemoveSpecialCharacters(feed.Title) + ".jpg";
                            bool result = UtilityService.DownloadImageFromUrl(imageUrl, imagepath);
                            if (result == true)
                            {

                                feed.ImageURL1 = UtilityService.RemoveSpecialCharacters(feed.Title) + ".jpg";
                            }
                            else
                            {
                                feed.ImageURL1 = null;
                            }
                        }
                        else
                        {
                            feed.ImageURL1 = null;
                        }
                        int Id = 0;
                        var feedlist = connection.GetAll<FeedPost>().ToList();
                        if (feedlist.Any())
                            Id = feedlist.Max(x => x.FeedPostId);
                        Id += 1;
                        feed.FeedPostId = Id;
                        connection.InsertOrUpdate(feed);
                    }

                }
            }

        }

        public static void GetRssFeedBySource(int id)
        {
            var sourcedetailconnection = new RepositoryBaseMongo("SourceDetail");
            var list = (from f in sourcedetailconnection.GetAll<SourceDetail>()
                where f.SourceId == id
                select f).ToList();

            var feedConnection = new RepositoryBaseMongo("FeedPost");

            foreach (var item in list)
            {
                var data = feedConnection.GetAll<FeedPost>().Select(x => x.SourceLink).ToList();
                var title = feedConnection.GetAll<FeedPost>().Select(x => x.Title).ToList();
                var rssFeeds = RssReader.GetRssFeed(item.FeedURL);

                if (rssFeeds != null)
                {
                    foreach (var rssFeed in rssFeeds.Where(x => !data.Contains(x.Link) && !title.Contains(x.Title)))
                    {
                        var connection = new RepositoryBaseMongo("FeedPost");
                        var feed = new FeedPost()
                        {
                            Title = rssFeed.Title,
                            ShortDescription = rssFeed.Description,
                            FullDescription = rssFeed.Description,
                            SourceId = item.SourceId,
                            CategoryId = item.CategoryId,
                            PublishDate = rssFeed.PublishDate,
                            SourceLink = rssFeed.Link,
                            ImageURL1 = rssFeed.ImageUrl,
                            IsActive = false
                        };
                        var imageUrl = UtilityService.GetImageUrlFromSite(feed.SourceLink);
                        if (imageUrl != null)
                        {
                            var path = AppDomain.CurrentDomain.BaseDirectory + "Content\\FeedImages\\";

                            if (!Directory.Exists(path))
                                Directory.CreateDirectory(path);
                            var imagepath = AppDomain.CurrentDomain.BaseDirectory + "Content\\FeedImages\\" +
                                            UtilityService.RemoveSpecialCharacters(feed.Title) + ".jpg";
                            bool result = UtilityService.DownloadImageFromUrl(imageUrl, imagepath);
                            if (result == true)
                            {

                                feed.ImageURL1 = UtilityService.RemoveSpecialCharacters(feed.Title) + ".jpg";
                            }
                            else
                            {
                                feed.ImageURL1 = null;
                            }
                        }
                        else
                        {
                            feed.ImageURL1 = null;
                        }
                        int Id = 0;
                        var feedlist = connection.GetAll<FeedPost>().ToList();
                        if (feedlist.Any())
                            Id = feedlist.Max(x => x.FeedPostId);
                        Id += 1;
                        feed.FeedPostId = Id;
                        connection.InsertOrUpdate(feed);
                    }
                }

            }
        }


        public static void GetFeedImage()
        {
            var connection = new RepositoryBaseMongo("FeedPost");
            var data = connection.GetAll<FeedPost>();
            foreach (var feed in data)
            {
                var imageUrl = UtilityService.GetImageUrlFromSite(feed.SourceLink);
                if (imageUrl != null)
                {
                    var path = AppDomain.CurrentDomain.BaseDirectory + "Content\\FeedImages\\";



                }
            }
        }

    }
}